﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.Collections;
using System.Reflection;


namespace MODULE_TIENICH
{
   public  class cls_report
    {
        private CrystalDecisions.CrystalReports.Engine.ReportDocument Report ;
        private CrystalDecisions.CrystalReports.Engine.ReportDocument crSubreport;
        public void Load_Report(string ReportName, DataTable dt)
        {
            Report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            try
            {
                Report.Load(ReportName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }

            Report.SetDataSource(dt);
            

            
            
        }
        public void Load_Report(string ReportName, DataSet dt)
        {
            Report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            try
            {
                Report.Load(ReportName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }

            Report.SetDataSource(dt);




        }
        public void Load_Report(string ReportName,IEnumerable iIEnumera)
        {
            Report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            try

            {
                
                
                Report.Load(ReportName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                MessageBox.Show("Không tìm thấy report.Kiểm tra lại đường dẫn");

            }
            try
            {

                DataTable dt = IEnumerabletoDataTable(iIEnumera);
                Report.SetDataSource(dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }



        }
        public DataTable IEnumerabletoDataTable(IEnumerable IEN)
        {
            DataTable dt=new DataTable();
            
            foreach(object obj in IEN)
            {
                Type t = obj.GetType();
                PropertyInfo[] pis = t.GetProperties();
                foreach (PropertyInfo pi in pis)
                {
                    if (!dt.Columns.Contains(pi.Name))
                    {
                        dt.Columns.Add(pi.Name);
                    }
                }
                DataRow dr = dt.NewRow();
                foreach (PropertyInfo pi in pis)
                {
                    object value=pi.GetValue(obj,null);
                    dr[pi.Name]=value;
                }
                dt.Rows.Add(dr);

            }
           
            return dt;

        }
        public void Formulas_Report(string FormulaField, string Value)
        {
            string UP = FormulaField.ToUpper();
            CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinitions crFormulaFieldDefinitions;
            CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition crFormulaFieldDefinition;
            int LoopFormula;
            String FormulaName;
            crFormulaFieldDefinitions = Report.DataDefinition.FormulaFields;
            crFormulaFieldDefinitions.Reset();
            for (LoopFormula = 0; LoopFormula <= crFormulaFieldDefinitions.Count - 1; LoopFormula++)
            {
                crFormulaFieldDefinition = crFormulaFieldDefinitions[LoopFormula];
                FormulaName = crFormulaFieldDefinition.Name;


                if (FormulaName != null)
                {
                    if (FormulaName.ToUpper() == UP)
                    {
                        crFormulaFieldDefinition.Text = Value;
                    }
                       
                }
            }
        }
        public void Load_SubReport(string SubReportName, DataTable dt)
        {
            crSubreport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            try
            {
                crSubreport = Report.OpenSubreport(SubReportName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            crSubreport.SetDataSource(dt);
           

        }
        public void Load_SubReport(string SubReportName, IEnumerable iIEnumerable)
        {
            try
            {
                crSubreport = Report.OpenSubreport(SubReportName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            try
            {
            DataTable dt = IEnumerabletoDataTable(iIEnumerable);
            crSubreport.SetDataSource(dt);
             }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }
         public void Formulas_SubReport(string FormulaField, string Value)
         {
             string UP = FormulaField.ToUpper();
             CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinitions crFormulaFieldDefinitions;
             CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition crFormulaFieldDefinition;
             int LoopFormula;
             String FormulaName;
             crFormulaFieldDefinitions = crSubreport.DataDefinition.FormulaFields;
             crFormulaFieldDefinitions.Reset();
             for (LoopFormula = 0; LoopFormula < crFormulaFieldDefinitions.Count - 1; LoopFormula++)
             {
                 crFormulaFieldDefinition = crFormulaFieldDefinitions[LoopFormula];
                 FormulaName = crFormulaFieldDefinition.Name;
                
                 if(FormulaName.ToUpper()==UP)
                 {
                     
                         crFormulaFieldDefinition.Text = Value;
                        
                 }
             }
         }
         public void Open_Report()
         {
             try
             {
                 frm_report frm = new frm_report();
                 frm.crystalReportViewer1.DisplayGroupTree = false;
                 Report.Refresh();
                 frm.crystalReportViewer1.ReportSource = Report;
                 frm.crystalReportViewer1.ShowTextSearchButton = true;

                 frm.WindowState = FormWindowState.Maximized;
                 frm.crystalReportViewer1.Refresh();
                 frm.crystalReportViewer1.EnableDrillDown = false;
                 frm.Show();
             }
             catch (Exception ex)
             {
             }
         }

    }
}
